﻿using System.Collections.Generic;
using System.Linq;
using compshop.Modules.Categories.Views;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;
using System.Windows;


namespace compshop.Modules.Categories.Presenter
{
    public class CategoriesPresenter
    {
        private readonly CompShopEntities contextDS;
        private IList<Category> categories;
        private EventsManager eventsManager;

        public IViewCategory View { get; set; }
        
        public IList<Category> CategoryList
        {
            get { return categories; }
            set { categories = value; }
        }

        public CategoriesPresenter(EventsManager eventsManager)
        {
            contextDS = CompShopEntitiesContext.GetInstance();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();

            categories=contextDS.Categories.Expand("SubCategories").ToList<Category>();                 
        }

        public void ShowAddCategoryDialog()
        {
            AddCategoryDialog addDialog = new AddCategoryDialog(true);
            addDialog.DataContext = this;
            if (addDialog.ShowDialog()==true)
            {
                Category category = new Category();
                category.Name = addDialog.tboxNewCategory.Text;
                contextDS.AddObject("Categories", category);

                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
                else
                {
                    categories = contextDS.Categories.Expand("SubCategories").ToList<Category>();
                    View.Presenter = this;
                }
            }
        }

        public void ShowAddSubCategoryDialog()
        {
            AddCategoryDialog addDialog = new AddCategoryDialog(false);
            addDialog.DataContext = this;

            if (addDialog.ShowDialog() == true)
            {
                Category cat = addDialog.comboxCategory.SelectedItem as Category;

                SubCategory subCategory = new SubCategory();
                subCategory.Name = addDialog.tboxNewCategory.Text;
                subCategory.CategoryID = cat.CategoryID;
                cat.SubCategories.Add(subCategory);
                contextDS.AddObject("SubCategories", subCategory);                  

                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
            }
        }

        public void ShowEditDialog(object ob)
        {
            if (ob is SubCategory)
            {
                SubCategory subCategory = ob as SubCategory;
                AddCategoryDialog addDialog = new AddCategoryDialog(null);
                addDialog.tboxNewCategory.Text = subCategory.Name;

                if (addDialog.ShowDialog() == true)
                {
                    subCategory.Name = addDialog.tboxNewCategory.Text;
                    contextDS.UpdateObject(subCategory);

                    if (CompShopEntitiesContext.Save() < 0)
                        MessageBox.Show(Resource.ErrorSaveChanges);
                    else
                    {
                        categories = contextDS.Categories.Expand("SubCategories").ToList<Category>();
                        View.Presenter = this;
                    }
                }
            }
            else
            {
                Category category = ob as Category;
                AddCategoryDialog addDialog = new AddCategoryDialog(null);
                addDialog.tboxNewCategory.Text = category.Name;

                if (addDialog.ShowDialog() == true)
                {
                    category.Name = addDialog.tboxNewCategory.Text;
                    contextDS.UpdateObject(category);

                    if (CompShopEntitiesContext.Save() < 0)
                        MessageBox.Show(Resource.ErrorSaveChanges);
                    else
                    {
                        categories = contextDS.Categories.Expand("SubCategories").ToList<Category>();
                        View.Presenter = this;
                    }
                }
                
            }
        }


        #region Publishers

        public void SubCategorySelectedPublisher(int id)
        {
            eventsManager.EventAggregator.GetEvent<SubCategorySelectedEvent>().Publish(id);
        }

        public void CategorySelectedPublisher(int id)
        {
            eventsManager.EventAggregator.GetEvent<CategorySelectedEvent>().Publish(id);
        }

        #endregion

        #region Handlers

        public void SubCategorySelectedEventHandler(int id)
        {

        }

        public void CategorySelectedEventHandler(int id)
        {
        }

        #endregion

    }
}


// var query=con.Execute<Category>(new Uri("Categories", UriKind.Relative));